﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace Vit.Common.Collections
{
    /// <summary>
    /// 支持Xml序列化的字典
    /// </summary>
    /// <typeparam name="TKey">字典键类型</typeparam>
    /// <typeparam name="TValue">字典值类型</typeparam>
    [XmlSchemaProvider("")]
    [XmlRoot("dictionary")]
    public class SerializableDictionary<TKey, TValue>
        : Dictionary<TKey, TValue>, IXmlSerializable
    {
        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        /// <summary>
        /// 读取Xml并进行反序列化。
        /// </summary>
        /// <param name="reader">xml reader</param>
        public void ReadXml(System.Xml.XmlReader reader)
        {
            XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
            XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
            bool wasEmpty = reader.IsEmptyElement;
            reader.Read();
            if (wasEmpty)
                return;
            while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
            {
                reader.ReadStartElement("item");
                reader.ReadStartElement("key");
                TKey key = (TKey)keySerializer.Deserialize(reader);
                reader.ReadEndElement();
                reader.ReadStartElement("value");
                TValue value = (TValue)valueSerializer.Deserialize(reader);
                reader.ReadEndElement();
                this.Add(key, value);
                reader.ReadEndElement();
                reader.MoveToContent();
            }
            reader.ReadEndElement();
        }

        /// <summary>
        /// 序列化后写入xml
        /// </summary>
        /// <param name="writer">xml writer</param>
        public void WriteXml(System.Xml.XmlWriter writer)
        {
            XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
            XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
            foreach (TKey key in this.Keys)
            {
                writer.WriteStartElement("item");
                writer.WriteStartElement("key");
                keySerializer.Serialize(writer, key);
                writer.WriteEndElement();
                writer.WriteStartElement("value");
                TValue value = this[key];
                valueSerializer.Serialize(writer, value);
                writer.WriteEndElement();
                writer.WriteEndElement();
            }
        }
        #endregion
        /*
        public static XmlQualifiedName DictionarySchema(XmlSchemaSet xss)
        {
            XmlSchema xs = XmlSchema.Read(new StringReader(
              "<xs:schema id='DictionarySchema' targetNamespace='" + NS +
              "' elementFormDefault='qualified' xmlns='" + NS + "' xmlns:mstns='" +
              NS + "' xmlns:xs='http://www.w3.org/2001/XMLSchema'><xs:complexType " +
              "name='DictionaryType'><xs:sequence><xs:element name='item' type='ItemType' " +
              "maxOccurs='unbounded' /></xs:sequence></xs:complexType><xs:complexType " +
              "name='ItemType'><xs:sequence><xs:element name='key' type='xs:string' />" +
              "<xs:element name='value' type='xs:string' /></xs:sequence></xs:complexType>" +
              "<xs:element name='dictionary' type='mstns:DictionaryType'></xs:element></xs:schema>"),
              null);
            xss.XmlResolver = new XmlUrlResolver();
            xss.Add(xs);
            return new XmlQualifiedName("DictionaryType", NS);
        }
 * */
    }
}
